package com.xying.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: Hisen
 * @Date: 2018/3/28 15:40
 * @Description:
 */
@Slf4j
public class JsonUtil {
  protected static ObjectMapper objectMapper = new ObjectMapper();
  static {
    //让所有的字段都加入序列化中
    objectMapper.setSerializationInclusion(Include.NON_EMPTY);
    //忽略空bean转化为json的错误
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    //忽略属性在json中存在，java对象中不存在的情况
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    //取消默认使用TimeStamp格式
    //objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
    //日期使用以下格式
    //objectMapper.setDateFormat(new SimpleDateFormat(DateTimeUtil.STANDARD_FORMAT));
  }

  public static JsonNode stingToNode(String str){
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = null;
    try {
      jsonNode = objectMapper.readTree(str);
    } catch (IOException e) {
      log.error("转化为Json节点出错", e);
    }

    return jsonNode;
  }

  public static <T> T readNodeValue(JsonNode jsonNode, Class<T> o){
    T result = null;
    try {
      result = jsonNode.traverse(objectMapper).readValueAs(o);
    } catch (IOException e) {
      log.error("读取节点值出错", e);
    }

    return result;
  }


  public static <T> String objToString(T o){
    if (o == null) {
      return null;
    }
    try {
      return o instanceof String ? (String) o : objectMapper.writeValueAsString(o);
    } catch (JsonProcessingException e) {
      log.warn("Parse Object to String error", e);
      return null;
    }
  }

  public static <T> String objToStringPretty(T o){
    if (o == null) {
      return null;
    }
    try {
      return o instanceof String ? (String) o : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
    } catch (JsonProcessingException e) {
      log.warn("Parse Object to String error", e);
      return null;
    }
  }

  public static <T> T stringToObj(String str, Class<T> clazz) {
    if (StringUtils.isEmpty(str) || clazz == null) {
      return null;
    }
    try {
      return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
    } catch (IOException e) {
      log.warn("Parse String to Object error", e);
      return null;
    }
  }

  public static <T> T stringToObj(String str, Class<?> collectionClass, Class<?>... elementClasses) {
    JavaType javaType = objectMapper.getTypeFactory().constructParametrizedType(collectionClass, collectionClass, elementClasses);
    try {
      return objectMapper.readValue(str, javaType);
    } catch (IOException e) {
      log.warn("Parse String to Object error", e);
      return null;
    }
  }

  public static <T> Object stringToObj(String str, TypeReference<T> tTypeReference) {
    if (StringUtils.isEmpty(str) || tTypeReference == null) {
      return null;
    }
    try {
      return tTypeReference.getType().equals(String.class) ? (T) str : objectMapper.readValue(str, tTypeReference);
    } catch (IOException e) {
      log.warn("Parse String to Object error", e);
      return null;
    }
  }
}
